package com.ashun.controller;

import com.rop.ServiceRouter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping(value = "/distributorApi")
public class FirstRopAccessController {
	
	@Autowired
    private ServiceRouter serviceRouter;

	/**
	 * 	将请求导向到Rop的框架中。
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/{version}/{category}/{partner}/{method}" ,method = RequestMethod.GET)
	public void get(@PathVariable String version, @PathVariable String category, @PathVariable String partner, @PathVariable String method,
                    HttpServletRequest request, Model model, HttpServletResponse response){
		request.setAttribute("v", version);
		request.setAttribute("method", category+"."+partner+"."+method);
		request.setAttribute("isGet","true");
		serviceRouter.service(request, response);
	}
	
	/**
	 * 	将请求导向到Rop的框架中。
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/{version}/{category}/{partner}/{method}" ,method = RequestMethod.POST)
	public void post(@PathVariable String version, @PathVariable String category, @PathVariable String partner, @PathVariable String method,
                     HttpServletRequest request, Model model, HttpServletResponse response){
		request.setAttribute("v", version);
		request.setAttribute("method", category+"."+partner+"."+method);
		request.setAttribute("isGet","false");
		serviceRouter.service(request, response);
	}
	
	/**
	 * 	将请求导向到Rop的框架中。
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/{version}/{category}/{method}/" ,method = RequestMethod.POST)
	public void post(@PathVariable String version, @PathVariable String category, @PathVariable String method,
                     HttpServletRequest request, Model model, HttpServletResponse response){
		request.setAttribute("v", version);
		request.setAttribute("method", category+"."+method);
		request.setAttribute("isGet","false");
		serviceRouter.service(request, response);
	}
	/**
	 * 	将请求导向到Rop的框架中。
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/{version}/{category}/{method}/" ,method = RequestMethod.GET)
	public void get(@PathVariable String version, @PathVariable String category, @PathVariable String method,
                    HttpServletRequest request, Model model, HttpServletResponse response){
		request.setAttribute("v", version);
		request.setAttribute("method", category+"."+method);
		request.setAttribute("isGet","true");
		serviceRouter.service(request, response);
	}
}
